package com.sqoq.downloader3.adapter_ddg

import android.content.Context
import androidx.databinding.ViewDataBinding
import com.bumptech.glide.Glide
import com.sqoq.downloader3.DownloadTool_ddg
import com.sqoq.downloader3.R
import com.sqoq.downloader3.base_ddg.BaseRecyclerViewAdapter_ddg
import com.sqoq.downloader3.bean_ddg.VideoListBean_ddg
import com.sqoq.downloader3.databinding.ItemDownloadDdgBinding
import com.sqoq.downloader3.videolist_ddg.VideoListActivity_ddg

class DownloadAdapter_ddg(context: Context) :
    BaseRecyclerViewAdapter_ddg<VideoListBean_ddg>(context) {
    override fun getItemLayout(viewType: Int): Int {
        return R.layout.item_download_ddg

    }

    fun generateTime(time: Long): String? {
        val totalSeconds = (time / 1000).toInt()
        val seconds = totalSeconds % 60
        val minutes = totalSeconds / 60 % 60
        val hours = totalSeconds / 3600
        return if (hours > 0) String.format(
            "%02d:%02d:%02d",
            hours,
            minutes,
            seconds
        ) else String.format("%02d:%02d", minutes, seconds)
    }

    override fun bindData(dataBinding: ViewDataBinding, data: VideoListBean_ddg, position: Int) {
        if (dataBinding is ItemDownloadDdgBinding) {
            Glide.with(dataBinding.root.context).load(data.filePath_ddg).into(dataBinding.imgDdg)
            dataBinding.textDdg.setText(data.title)
            dataBinding.deleteDdg.setOnClickListener {
                DownloadTool_ddg.delete(data)
                remove(data)
                notifyDataSetChanged()
            }
            dataBinding.timeDdg.setText(generateTime(data.duration))
            dataBinding.root.setOnClickListener {
                VideoListActivity_ddg.start(dataBinding.root.context, data)
            }
        }

    }
}